package xyz.xuminghai.javafx_learn.scene.control;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.List;

/**
 * 2023/1/17 10:37 星期二<br/>
 * <p>
 * 向用户显示选项列表的对话框，他们最多可以从中选择一个项目。
 *
 * @author xuMingHai
 */
public class ChoiceDialogApp extends Application {

    private final AnchorPane anchorPane = new AnchorPane();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        primaryStage.setScene(new Scene(anchorPane, 800, 600));

        Background background1 = new Background(new BackgroundFill(Color.RED, null, null));
        Background background2 = new Background(new BackgroundFill(Color.BLUE, null, null));
        Background background3 = new Background(new BackgroundFill(Color.GREEN, null, null));

        // 对话框类只能在javafx线程创建，用于关联 anchorPane 背景色
        ChoiceDialog<Background> choiceDialog = new ChoiceDialog<>(background1,
                List.of(background1, background2, background3));
        // TODO: 没有找到StringConverter
        choiceDialog.setTitle("选择背景色");

        Button button = new Button("Show ChoiceDialog");
        button.setOnAction(event -> choiceDialog.showAndWait()
                .ifPresent(anchorPane::setBackground));

        anchorPane.getChildren().add(button);
        anchorPane.setBackground(background1);

        primaryStage.show();
    }
}
